package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commos.dto.PreOrderDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.form.UserInsertPreOrderForm;
import com.woniu108.product.param.*;
import com.woniu108.product.service.PreOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务预约表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/preOrder")
@Api(tags = "服务预约接口")
public class PreOrderController {
    @Resource
    private PreOrderService preOrderService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 门店查询需处理预约
     * @return 门店查询需处理预约详情
     */
    @GetMapping("/findStorePreOrder.st")
    @ApiOperation("门店查询需处理预约")
    public Result<List<PreOrderDto>> findStorePreOrder(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        List<PreOrderDto> list = preOrderService.findStorePreOrder(storeAdminId);
        return Result.newListInstance(PreOrderDto.class).setData(list).setMsg("门店查询需处理预约成功");
    }
    /**
     * 查询用户预约
     * @return 预约详情
     */
    @GetMapping("/findAllUserPreOrder")
    @ApiOperation("查询用户预约")
    public Result<List<PreOrderDto>> findAllUserPreOrder(@RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        List<PreOrderDto> list = preOrderService.findAllUserPreOrder(userId);
        return Result.newListInstance(PreOrderDto.class).setData(list).setMsg("查询用户预约成功");
    }
    /**
     * 门店同意预约
     * @param preOrderId 预约编号
     * @return boolean值
     */
    @GetMapping("/storeAgreePreOrder.st")
    @ApiOperation("门店同意预约")
    @ApiImplicitParam(value = "preOrderId",name = "服务预约编号")
    public Result<Boolean> storeAgreePreOrder(String preOrderId,@RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        StoreAgreePreOrderParam param=new StoreAgreePreOrderParam(preOrderId,storeAdminId);
        Boolean bool=preOrderService.storeAgreePreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("门店同意预约成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("门店同意预约失败").setData(false).setCode(500);

    }
    /**
     * 门店不同意预约
     * @param preOrderId 预约编号
     * @return boolean值
     */
    @GetMapping("/storeDisAgreePreOrder.st")
    @ApiOperation("门店不同意预约")
    @ApiImplicitParam(value = "preOrderId",name = "服务预约编号")
    public Result<Boolean> storeDisAgreePreOrder(String preOrderId,@RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        StoreAgreePreOrderParam param=new StoreDisAgreePreOrderParam(preOrderId,storeAdminId);
        Boolean bool=preOrderService.storeDisAgreePreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("门店不同意预约成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("门店不同意预约失败").setData(false).setCode(500);
    }
    /**
     * 用户取消预约
     * @param preOrderId 预约编号
     * @return boolean值
     */
    @GetMapping("/userCancelPreOrder")
    @ApiOperation("用户取消预约")
    @ApiImplicitParam(value = "preOrderId",name = "服务预约编号")
    public Result<Boolean> userCancelPreOrder(String preOrderId,@RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        UserCancelPreOrderParam param=new UserCancelPreOrderParam(preOrderId,userId);
        Boolean bool=preOrderService.userCancelPreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("用户取消预约成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("用户取消预约失败").setData(false).setCode(500);
    }
    /**
     * 预约完成
     * @param preOrderId 预约编号
     * @return boolean值
     */
    @GetMapping("/finishPreOrder.c")
    @ApiOperation("到店，预约完成")
    @ApiImplicitParam(value = "preOrderId",name = "服务预约编号")
    public Result<Boolean> finishPreOrder(String preOrderId,@RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        String storeAdminId = JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
        FinishPreOrderParam param=new FinishPreOrderParam(preOrderId,userId,storeAdminId);
        Boolean bool=preOrderService.finishPreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("预约完成成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("预约完成失败").setData(false).setCode(500);
    }
    /**
     * 用户删除处理完的预约订单
     * @param preOrderId 预约编号
     * @return boolean值
     */
    @GetMapping("/userDeletePreOrder")
    @ApiOperation("用户删除处理完的预约订单")
    @ApiImplicitParam(value = "preOrderId",name = "服务预约编号")
    public Result<Boolean> userDeletePreOrder(String preOrderId,@RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        UserDeletePreOrderParam param=new UserDeletePreOrderParam(preOrderId,userId);
        Boolean bool=preOrderService.userDeletePreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("用户删除处理完的预约订单成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("用户删除处理完的预约订单失败").setData(false).setCode(500);
    }

    /**
     * 用户新增订单
     * @param form 预约信息
     * @return boolean值
     */
    @PostMapping("/userInsertPreOrder")
    @ApiOperation("用户新增订单")
    public Result<Boolean> userInsertPreOrder(@RequestBody UserInsertPreOrderForm form, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String userId = JWTUtil.parseUserToken(refreshToken).get("userId").toString();
        UserInsertPreOrderParam param= BeanUtil.toBean(form,UserInsertPreOrderParam.class);
       param.setUserId(userId);
        Boolean bool=preOrderService.userInsertPreOrder(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("用户新增订单成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("用户新增订单失败").setData(false).setCode(500);
    }
}

